﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ClientBasicData.Infrastructure.Interface;

namespace ClientBasicData.Infrastructure.Implement
{
    public class BaseRespository<T> : IBaseRespository<T> where T : class
    {
        private readonly MyDBContext context;

        public BaseRespository(MyDBContext context)
        {
            this.context = context;
        }
        /// <summary>
        /// 新增
        /// </summary>
        /// <param name="t"></param>
        /// <returns></returns>
        public async Task<int> AddAsync(T t)
        {
            await context.Set<T>().AddAsync(t);
            return await context.SaveChangesAsync();
        }
        /// <summary>
        /// 获取所有
        /// </summary>
        /// <returns></returns>
        public IQueryable<T> GetAll()
        {
            return context.Set<T>();
        }
        /// <summary>
        /// 根据id查询
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<T> GetModelAsync(long id)
        {
            return await context.Set<T>().FindAsync(id);
        }
        /// <summary>
        /// 更新
        /// </summary>
        /// <param name="t"></param>
        /// <returns></returns>
        public Task<int> Update(T t)
        {
            context.Set<T>().Update(t);
            return context.SaveChangesAsync();
        }
    }
}
